package cn.itcast.documentapis;

import org.elasticsearch.action.get.GetResponse;
import org.elasticsearch.action.get.MultiGetItemResponse;
import org.elasticsearch.action.get.MultiGetResponse;
import org.elasticsearch.client.transport.TransportClient;
import org.junit.jupiter.api.Test;

/**
 * @Description TODO : multi get API允许根据索引和id获取文档列表：
 * @Author: zj
 * @Date: 14:00 2019/3/8
 * @Version 1.0
 **/
public class MultiGetAPI {
    @Test
    public void mulitGet() {
        TransportClient client = esClient.getClient();
        //根据索引 type id  去查询数据 可以查询多个不同的 索引
        MultiGetResponse multiGetItemResponses = client.prepareMultiGet()
                .add("twitter", "_doc", "1")
                .add("twitter", "_doc", "2", "3", "4")
                .add("hi", "_doc", "1")
                .get();
        for (MultiGetItemResponse itemResponse : multiGetItemResponses) {
            GetResponse response = itemResponse.getResponse();
            //您可以检查文档是否存在
            if (response.isExists()) {
                String sourceAsString = response.getSourceAsString();
                System.out.println(sourceAsString);
            }
        }


    }
}
